﻿using System.Collections.Generic;
using Newtonsoft.Json;
using OrkutAPILibrary.Constants;
using OrkutAPILibrary.Entities;

namespace OrkutAPILibrary.Methods
{
    /// <summary>
    /// Orkut Video Factory
    /// </summary>
    static class OrkutVideoFactory
    {
        /// <summary>
        /// Gets the self videos.
        /// </summary>
        /// <param name="count">No of videos to return.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutVideo> GetSelfVideos(int count = OrkutLibraryConstants.DefaultNoOfVideos)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "videos.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"pageType", "first"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutVideo>>(requestResponse);
        }

        /// <summary>
        /// Gets the next set of self videos.
        /// </summary>
        /// <param name="lastVideoId">The last video id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutVideo> GetNextSetOfSelfVideos(string lastVideoId, int count = OrkutLibraryConstants.DefaultNoOfVideos)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "videos.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"pageType", "next"},
                                    {"lastKey", lastVideoId},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutVideo>>(requestResponse);
        }

        /// <summary>
        /// Gets the previous set of self videos.
        /// </summary>
        /// <param name="firstVideoId">The first video id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutVideo> GetPreviousSetOfSelfVideos(string firstVideoId, int count = OrkutLibraryConstants.DefaultNoOfVideos)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "videos.get"},
                                    {"userId", "@me"},
                                    {"groupId", "@self"},
                                    {"pageType", "prev"},
                                    {"lastKey", firstVideoId},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutVideo>>(requestResponse);
        }

        /// <summary>
        /// Gets the videos for user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="count">No of videos to return.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutVideo> GetVideosForUser(string userId, int count = OrkutLibraryConstants.DefaultNoOfVideos)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "videos.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"pageType", "first"},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutVideo>>(requestResponse);
        }

        /// <summary>
        /// Gets the next set of videos for user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="lastVideoId">The last video id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutVideo> GetNextSetOfVideosForUser(string userId, string lastVideoId, int count = OrkutLibraryConstants.DefaultNoOfVideos)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "videos.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"pageType", "next"},
                                    {"lastKey", lastVideoId},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutVideo>>(requestResponse);
        }

        /// <summary>
        /// Gets the previous set of videos for user.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <param name="firstVideoId">The first video id.</param>
        /// <param name="count">The count.</param>
        /// <returns></returns>
        internal static OrkutData<OrkutVideo> GetPreviousSetOfVideosForUser(string userId, string firstVideoId, int count = OrkutLibraryConstants.DefaultNoOfVideos)
        {
            var extraData = new Dictionary<string, string>
                                {
                                    {"method", "videos.get"},
                                    {"userId", userId},
                                    {"groupId", "@self"},
                                    {"pageType", "prev"},
                                    {"lastKey", firstVideoId},
                                    {"count", count.ToString()},
                                };

            var requestResponse = OrkutLibrary.ExecuteRequest(extraData);

            return string.IsNullOrEmpty(requestResponse)
                       ? null
                       : JsonConvert.DeserializeObject<OrkutData<OrkutVideo>>(requestResponse);
        }
    }
}
